package com.kgc.crowd.distributioncrowdcommon.util;

import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClientBuilder;
import com.aliyun.oss.OSSException;

import java.io.InputStream;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.UUID;

public class UploadUtil {
    public static void uploadSingFile(
            String endpoint,
            String accessKeyId,
            String accessKeySecret,
            String fileName,
            String folderName,
            String bucketName,
            InputStream inputStream){
        try {
            // 创建OSSClient实例。
            OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);
            //上传文件流
            String ObjectName=folderName+"/"+fileName;
            ossClient.putObject(bucketName, ObjectName, inputStream);
            // 关闭OSSClient。
            ossClient.shutdown();
        }catch (OSSException e){
            e.printStackTrace();
            throw new RuntimeException(e.getMessage());
        }
    }
    /**
     * 生成文件名
     * @param originalFileName 原始文件名
     * @return
     */
    public static String generateFileName(String originalFileName) {

        // 截取扩展名部分
        String extensibleName = "";

        if(originalFileName.contains(".")) {
            extensibleName = originalFileName.substring(originalFileName.lastIndexOf("."));
        }
        return UUID.randomUUID().toString().replaceAll("-", "")+extensibleName;
    }

    /**
     * 根据日期生成目录名称
     * @return
     */
    public static String generateDayFolderName() {

        return new SimpleDateFormat("yyyyMMdd").format(new Date());
    }

}
